<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var str = 'djfTMDfjdsklMDklfdMMP'
    var reg = /(TMD)|(MD)|(MMP)/ig
    str = str.replace(reg, function (s) {
      console.log(s)
      var res = ''
      // s就是正在匹配的关键字
      // 可以根据关键字的长度来拼接*的数量
      for (var i = 0; i < s.length; i++) {
        res += '*'
      }
      // 返回值就是要替换的结果
      return res
    })
    console.log(str)
  </script>
</body>
</html>